from django.shortcuts import render
from django.views.generic import TemplateView

from commodity.models import CommodityInfos, Types


# Create your views here.
def indexView(request):
    """
    处理首页请求
    """
    title = "首页" #页面标题
    # 当前网页为首页时，首页的class='active'
    # 当前网页为所有商品时，首页的class='active'
    classContent = "" #用于页面样式控制的变量
    # 每日必抢内容（销量前8的商品显示）
    commodityInfos = CommodityInfos.objects.order_by('-sold').all()[:8]
    # 获取所有的商品分类
    types = Types.objects.all()
    # 筛选儿童服饰分类下的二级分类，以列表形式返回
    c1 = [x.seconds for x in types if x.firsts == '儿童服饰']
    # 获取其中销量前五
    clothes = CommodityInfos.objects.filter(types__in=c1).order_by('-sold').all()[:5]
    # 筛选奶粉辅食
    f1 = [x.seconds for x in types if x.firsts == '奶粉辅食']
    food = CommodityInfos.objects.filter(types__in=f1).order_by('-sold').all()[:5]
    # 筛选儿童用品
    g1 = [x.seconds for x in types if x.firsts == '儿童用品']
    goods = CommodityInfos.objects.filter(types__in=g1).order_by('-sold').all()[:5]
    # 将所有变量传递给模板进行渲染
    return render(request, 'index.html', locals())

class indexClassView(TemplateView):
    # 指定渲染模板
    template_name = 'index.html'
    # 可选，指定模板引擎
    template_engine = None
    # 可选，指定响应的Content-type
    content_type = None
    extra_context = {'title':'首页','classContent':''}
    def get_context_data(self, **kwargs):
        # 获取父类的上下文
        context = super(indexClassView, self).get_context_data(**kwargs)
        # 获取销量前八的商品
        context['commodityInfos'] = CommodityInfos.objects.order_by('-sold').all()[:8]
        # 获取所有商品分类
        types = Types.objects.all()
        # 筛选商品
        c1 = [x.seconds for x in types if x.firsts == '儿童服饰']
        context['clothes'] = CommodityInfos.objects.filter(types__in=c1).order_by('-sold').all()[:5]
        f1 = [x.seconds for x in types if x.firsts == '奶粉辅食']
        context['food'] = CommodityInfos.objects.filter(types__in=f1).order_by('-sold').all()[:5]
        g1 = [x.seconds for x in types if x.firsts == '儿童用品']
        context['goods'] = CommodityInfos.objects.filter(types__in=g1).order_by('-sold').all()[:5]
        return context

    def get(self, request, *args, **kwargs):
        """
        处理HTTP GET请求
        request代表HTTP请求信息
        如果路由设置了路由变量，那么可以从参数kwargs中获取
        """
        #获取上下文数据
        context = self.get_context_data(**kwargs)
        #渲染模板并返回响应
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        """
        处理HTTP POST请求
        """
        context = self.get_context_data(**kwargs)
        return self.render_to_response(context)

    def page_not_found(request, exception):
        return render(request,'404.html',status=404)

    def page_error(request):
        return render(request,'404.html',status=500)




